import Vue from 'vue'
import Vuex from 'vuex'
Vue.use(Vuex)
import {
	http
} from "../common/http.js"

export const store = new Vuex.Store({
	state: {
		userInfo: uni.getStorageSync('userInfo') || {},
		token: uni.getStorageSync('token') || '',
		
		
		detail: uni.getStorageSync('detail') || {}, //24小时内最新市场概
		symbol: uni.getStorageSync('symbol') || {}, //1day k线数据
		rate: uni.getStorageSync('rate') || 1, //汇率
		phone: uni.getStorageSync('phone') || 1, 
	},
	mutations: {
		getUserInfo(state, info) {
			console.log(state);
			state.userInfo = info
			state.token = state.userInfo.token
			uni.setStorageSync('userInfo', state.userInfo)
			uni.setStorageSync('token', state.userInfo.token)
			uni.setStorageSync('phone', state.userInfo.phone)
		},
		detail(state, msg) {
			// console.log('啊啊啊啊啊啊啊啊',msg.ch.split('.')[1]);
			if(msg.ch){
				state.detail[msg.ch.split('.')[1]] = msg.tick
			}
			// console.log('详情',state.detail);
			uni.setStorageSync('detail', state.detail)
		},
		symbol(state, msg) {
			// console.log('啊啊啊啊啊啊啊啊',msg.ch.split('.')[1]);
			if(msg.ch){
				state.symbol[msg.ch.split('.')[1]] = msg.tick
			}
			// console.log('详情',state.detail);
			uni.setStorageSync('symbol', state.symbol)
		},
		saveRate(state, rate) {
			state.rate = rate
			uni.setStorageSync('rate', rate)
		},
		exitLogin(state) {
			state.userInfo = {}
			state.token = ''
			uni.setStorageSync('userInfo', state.userInfo)
			uni.setStorageSync('token', state.userInfo.token)
		},
	},
	getters: {},
	// this.$store.dispatch('updateUserInfo')
	actions: {
		updateUserInfo({
			commit
		}) {
			http('/api/user/userInfo').then(data => {
				commit('getUserInfo', data)
				uni.stopPullDownRefresh();
			})
		}
	}
})
